package com.tmeone.erpone.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.entity.*;
import com.tmeone.erpone.service.ISCompanyService;
import com.tmeone.erpone.service.ITDepartmentService;
import com.tmeone.erpone.service.ITPostService;
import com.tmeone.erpone.service.ITStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
public class TStaffController {

    @Autowired
    private ITStaffService staffService;

    @Autowired
    private ITPostService postService;

    @Autowired
    private ITDepartmentService departmentService;

    @Autowired
    private ISCompanyService companyService;



    /**
     * 分页查询员工表
     * @param current
     * @return
     */
    @ResponseBody
    @RequestMapping("/staffs/{current}/{input}/{dId}/{pageSize}")
    private IPage<TStaff> selectListStaff(@PathVariable("current")Integer current,@PathVariable("input") String name,@PathVariable("dId") String dId ,@PathVariable("pageSize") Integer pageSize) {

        Page<TStaff> tStaffPage = new Page<>(current, pageSize);
        if (name.equals("null")  && dId.equals("null") ){

        IPage<TStaff> page = staffService.getPageTStaff(tStaffPage);
        System.out.println("dsadsa:::"+page.getRecords().toString());
        return page;
        }
        else {
                
            IPage<TStaff> page = staffService.getpageStaffTops(tStaffPage, name, dId);
            return  page;
        }

    }

    /**
     * 添加员工
     * @param staff
     * @return
     */

    @PostMapping("/staffs")
    private JsonResult addStaff(@RequestBody TStaff staff){
        staff.setId(String.valueOf(new Date().getTime()));
        //添加当前时间

        staff.setEntryTime(LocalDateTime.now());
        staff.setNo(String.valueOf(new Date().getTime()));
        staffService.save(staff);
        return new JsonResult(1, null);
    }

    /**
     * 更新员工
     * @param staff
     * @return
     */
    @PutMapping("/staffs")
    private JsonResult updataStaff(@RequestBody TStaff staff){
        //添加当前时间
        staff.setEntryTime(LocalDateTime.now());
        staffService.updateById(staff);
        return new JsonResult(1, null);
    }

    //加载所有的职位
    @GetMapping("/AllPostName")
    private  JsonResult allPostName(){
        List<TPost> list = postService.list();
        return new JsonResult(1,list);
    }

    //加载所有的子部门
    @GetMapping("/AllDepName")
    private JsonResult allDepName(){
        List<TDepartment> list = departmentService.list(new QueryWrapper<TDepartment>().lambda().ne(TDepartment::getPid,0));
        return  new JsonResult(1,list);
    }

    //加载所有的企业名称
    @GetMapping("/AllCompareName")
    private JsonResult allCompare(){
        List<SCompany> list = companyService.list();

        return  new JsonResult(1,list);
    }


    //改变状态
    @PutMapping("/staffsToStatus/{id}")
    private JsonResult  sToStatus(@PathVariable("id") String id){

        TStaff one = staffService.getOne(new QueryWrapper<TStaff>().lambda().eq(TStaff::getId, id));
        if (one.getAccountStatus().equals("1")){
            one.setAccountStatus("-1");
        }else {
            one.setAccountStatus("1");
        }

        staffService.updateById(one);

        return  new JsonResult(1,null);

    }

    /**
     * 部门to员工
     */
    @GetMapping("/AllPostNameByPid/{val}")
    private  JsonResult AllDepNameByPid(@PathVariable("val") String id ){

        List<TPost> list = postService.list(new QueryWrapper<TPost>().lambda().eq(TPost::getDId,id));

        return  new JsonResult(1,list);

    }
}
